package com.rongji.cms.tools.es.controller;

import com.rongji.cms.tools.es.domain.dto.CmsArticleDto;
import com.rongji.cms.tools.es.domain.entity.CmsArticle;
import com.rongji.cms.tools.es.mapper.mapstruct.ICmsArticleMapper;
import com.rongji.cms.tools.es.service.CmsArticleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * cms文章索引相关
 * @author yang
 * @date 2022/10/21 14:21
 * @description
 */
@RestController
@Slf4j
@RequestMapping("/cmsArticle")
public class CmsArticleController {

    @Resource
    private CmsArticleService articleService;

    /**
     * 批量插入
     *
     * @param cmsArticleDtoList
     * @return
     */
    @PostMapping("/batchInsert")
    public int batchInsert(@RequestBody List<CmsArticleDto> cmsArticleDtoList) {
//        log.info("cmsArticleDtoList；" + cmsArticleDtoList);
//        先转成es需要的entity
        List<CmsArticle> cmsArticleList = ICmsArticleMapper.INSTACT.listConvert(cmsArticleDtoList);

        return articleService.batchInsert(cmsArticleList);
    }


    /**
     * 通过id批量删除
     *
     * @param arIds
     */
    @PostMapping("/batchDeleteByIds")
    public void batchDeleteByIds(@RequestBody List<String> arIds) {
        articleService.batchDeleteByIds(arIds);

    }

    /**
     * 通过参数查询（eq方式）得到结果总数
     *
     * @param map
     * @return
     */
    @PostMapping("/searchNumByAllEq")
    public long searchNumByAllEq(@RequestBody Map<String, Object> map) {
        log.info("map:" + map);
        return articleService.searchNumByAllEq(map);
    }

    /**
     * 通过参数查询（eq方式）得到详细数据
     *
     * @param map
     * @return
     */
    @PostMapping("/searchByAllEq")
    public List<CmsArticle> searchByAllEq(@RequestBody Map<String, Object> map) {
        log.info("map:" + map);
        return articleService.searchByAllEq(map);
    }

    /**
     * 批量查询文章id
     *
     * @param params
     * @return
     */
    @PostMapping("/getArIds")
    public List<String> getArIds(@RequestBody Map<String, Object> params) {
        Map<String, Object> map = new HashMap<>();
        int pageSize = Integer.parseInt((String) params.get("pageSize"));
        params.remove("pageSize");
        map.putAll(params);
        return articleService.getArIds(map, pageSize);
    }
}
